package com.aiti.lulian.controller.gov;

import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.gov.CardGovProjectApplicationDto;
import com.aiti.lulian.dto.gov.CardGovProjectDto;
import com.aiti.lulian.entity.gov.CardGovProject;
import com.aiti.lulian.service.ICardGovProjectApplicationService;
import com.aiti.lulian.service.ICardGovProjectService;
import com.aiti.lulian.vo.gov.CardGovProjectVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "政务名片-项目控制层")
@RequestMapping("/gov/project")
public class CardGovProjectController extends WebController {

    @Autowired
    private ICardGovProjectService cardGovProjectService;

    @Autowired
    private ICardGovProjectApplicationService cardGovProjectApplicationService;

    /**
     * 支持分页
     */
    @ApiOperation("获取项目列表")
    @GetMapping("/list")
    public Message getProjectList(CardGovProjectDto dto) {
        Integer pageNum = dto.getPageNum();
        Integer pageSize = dto.getPageSize();
        if (pageNum != 0) {
            Page<CardGovProject> page = startPage(pageNum, pageSize);
            List<CardGovProjectVo> resultList = cardGovProjectService.getProjectList(dto, page);
            return Success(getPageResult(resultList, pageNum, pageSize, page.getTotal()));
        }
        return Success(cardGovProjectService.getProjectList(dto, null));
    }

    @ApiOperation("获取项目详情")
    @GetMapping("/{baseId}")
    public Message getProjectDetails(@PathVariable("baseId") String baseId) {
        return Success(cardGovProjectService.getProjectDetails(baseId));
    }

    @ApiOperation("新增项目")
    @PostMapping
    public Message addProject(@RequestBody CardGovProjectDto dto) {
        dto.setUserId(findUserId());
        cardGovProjectService.addProject(dto);
        return Message.success();
    }

    @ApiOperation("修改项目")
    @PutMapping
    public Message updateProject(@RequestBody CardGovProjectDto dto) {
        dto.setUserId(findUserId());
        cardGovProjectService.updateProject(dto);
        return Message.success();
    }

    @ApiOperation("删除项目")
    @DeleteMapping
    public Message deleteProject(CardGovProjectDto dto) {
        return cardGovProjectService.deleteProject(dto);
    }

    @ApiOperation("立即申报（项目）")
    @PostMapping("apply")
    public Message applyProject(@RequestBody CardGovProjectApplicationDto dto) {
        dto.setUserId(findUserId());
        cardGovProjectApplicationService.applyProject(dto);
        return Message.success();
    }

    @ApiOperation("获取项目申报信息")
    @GetMapping("/apply/{baseId}")
    public Message getApplyDetails(@PathVariable("baseId") String baseId) {
        return Success(cardGovProjectApplicationService.getApplyDetails(baseId));
    }
}
